package com.tangsm.api.demo.controller;

import com.tangsm.api.demo.domain.Result;
import com.tangsm.api.demo.enums.ResultCode;
import com.tangsm.api.demo.exception.ResultException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 订单 API 测试示例
 *
 * @author 湯神码
 * @date 2020/10/23
 */
@RestController
@RequestMapping("/orders")
public class OrderController {
    private static final HashMap<String, Object> INFO;
    static {
        INFO = new HashMap<>();
        INFO.put("orderId", "20201023001");
        INFO.put("orderTime", LocalDateTime.now());
        INFO.put("describe", "衣服");
    }

    /**
     * 获取订单信息
     *
     * @return Result
     */
    @GetMapping("/get")
    public Result<HashMap<String, Object>> getOrder() {
        return new Result<>(ResultCode.SUCCESS, INFO);
    }

    /**
     * 获取订单信息-返回改造
     *
     * @return Result
     */
    @GetMapping("/get1")
    public Result<HashMap<String, Object>> getOrder1() {
        if("2020-10-23".equals(LocalDate.now())){
            System.out.println("1024 节日快乐");
        }
        return Result.success(INFO);
    }

    /**
     * 获取订单信息-返回注解改造
     *
     * @return Result
     */
    @GetMapping("/get2")
    public HashMap<String, Object> getOrder2(){
        return INFO;
    }

    /**
     * 系统异常
     * @return
     * @throws Exception
     */
    @GetMapping("error")
    public Map<String, Object> helloError() throws Exception {
        throw new Exception("helloError");
    }

    /**
     * 自定义异常
     * @return
     * @throws Exception
     */
    @GetMapping("myError")
    public Map<String, Object> helloMyError() throws Exception {
        throw new ResultException();
    }
}
